package org.dce.screencapturer.actions;

import java.awt.Point;
import java.awt.event.ActionEvent;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.dce.screencapturer.actions.objects.ObjectAction;

@SuppressWarnings("serial")
public abstract class DropDownAction 
	extends ObjectAction
	implements ListSelectionListener{
	
	private final JList list = new JList();
	private Popup popup;

	public DropDownAction(String category, String actionId) {
		this(category,actionId, null);
	}
	
	public DropDownAction(String category, String actionId, Icon icon) {
		super(category, actionId, icon);
		
		list.addListSelectionListener(this);
	}
	
	protected void setModel(ListModel listModel){
		this.list.setModel(listModel);
	}

	protected void setRenderer(ListCellRenderer renderer){
		this.list.setCellRenderer(renderer);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (popup == null){
			showPopup(e);
		} else {
			destroyPopup();
		}
	}

	private void showPopup(ActionEvent e) {
		JComponent source = (JComponent)e.getSource();
		
		Point locationOnScreen = source.getLocationOnScreen();
		
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setSize(120,250);
		popup = PopupFactory.getSharedInstance().getPopup(
				source, 
				scrollPane, 
				locationOnScreen.x, 
				locationOnScreen.y + source.getHeight());
		popup.show();
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		
		if (!e.getValueIsAdjusting()){
			itemSelected(list.getSelectedValue());
			destroyPopup();
		}
	}

	private void destroyPopup() {
		popup.hide();
		popup = null;
	}
	
	
	protected abstract void itemSelected(Object object);
//
//	
//	private class Grid 
//		extends JPanel{
//		
//		private ListModel model;
//		private ListCellRenderer renderer = new DefaultListCellRenderer();
//		
//		public Grid(int columns){
//			setLayout(new GridLayout(1, columns));
//		}
//		
//		public void setModel(ListModel model){
//			this.model = model;
//			
//			update();
//		}
//		
//		public void setRenderer(ListCellRenderer renderer){
//			this.renderer = renderer;
//			for (Component c:getComponents()){
//				((GridItem)c).setRenderer(renderer);
//			}
//		}
//		
//		private void update(){
//			removeAll();
//			
//			for (int i=0;i<model.getSize();i++){
//				GridItem item = new GridItem(model.getElementAt(i));
//				item.setRenderer(renderer);
//				add(item);
//			}
//		}
//		
//		
//	}
//	
//	private class GridItem 
//		extends JComponent{
//		
//		private Object item;
//		private ListCellRenderer renderer;
//
//		public void setRenderer(ListCellRenderer renderer){
//			this.renderer = renderer;
//		}
//		
//		public GridItem(Object item){
//			this.item = item;
//		}
//	}
}
